package com.mrduan.calculator.ui.calculator.sell;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mrduan.calculator.R;
import com.mrduan.calculator.ui.calculator.base.CalculatorBean;
import com.mrduan.calculator.ui.calculator.base.CalculatorHandler;
import com.mrduan.calculator.ui.calculator.base.CalculatorListener;
import com.shike.baselibrary.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class SellCalculatorActivity extends BaseActivity implements CalculatorListener {

    private TextView mResult,mPayWay,mType,mInput;
    private ListView mListView;
    private GridView mHorizontalGridview;

    private SellCalculatorAdapter mAdapter;

    private CalculatorHandler calculatorHandler ;

    private List<String> titles = new ArrayList<>();

    @Override
    public int getContentView() {
        return R.layout.activity_calculator;
    }


    @Override
    public void initBindWidget() {
        mResult = findViewById(R.id.showResult);
        mPayWay = findViewById(R.id.way);
        mType = findViewById(R.id.type);
        mInput = findViewById(R.id.content);

        mListView = findViewById(R.id.list_show);

        mHorizontalGridview = findViewById(R.id.horizontal_gridview);

    }

    @Override
    public void initListener() {
        mHorizontalGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i(getClass().getName(),titles.get(position));
            }
        });
    }

    @Override
    public void initData() {
        calculatorHandler = new CalculatorHandler(findViewById(R.id.keyboard));
        calculatorHandler.setmListener(this);

        mAdapter = new SellCalculatorAdapter(this,calculatorHandler.inputArray,null);
        mListView.setAdapter(mAdapter);

        titles.add("上衣");
        titles.add("裤子");
        titles.add("玩具玩具");
        titles.add("帽子");
        titles.add("袜子");
        titles.add("零食");
        titles.add("其他");
        titles.add("上衣");
        titles.add("裤子");
        titles.add("玩具");
        titles.add("帽子");
        titles.add("袜子");
        titles.add("零食");
        titles.add("其他");

        int size = titles.size();
        int length = 120;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int gridviewWidth = (int) (size * length * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        mHorizontalGridview.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        mHorizontalGridview.setColumnWidth(itemWidth); // 设置列表项宽
        mHorizontalGridview.setNumColumns(size); // 设置列数量=列表集合数
        mHorizontalGridview.setGravity(Gravity.CENTER);

        HorizontalGridViewAdapter adapter = new HorizontalGridViewAdapter();
        mHorizontalGridview.setAdapter(adapter);

    }

    class HorizontalGridViewAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public Object getItem(int position) {
            return titles.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView typeText = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(SellCalculatorActivity.this).inflate(R.layout.horizontal_calculator_type_adapter,null);

                typeText = convertView.findViewById(R.id.horizontal_calculator_type);

                convertView.setTag(typeText);
            } else {
                typeText = (TextView) convertView.getTag();
            }

            typeText.setText(titles.get(position));

            return convertView;
        }
    }

    /*---------------监听信息-----------------------------------------*/

    @Override
    public void doSave(CalculatorBean bean) {

    }

    @Override
    public void doDelete(CalculatorBean bean) {

    }

    @Override
    public void doClear(CalculatorBean bean) {

    }

    @Override
    public void upView(List<CalculatorBean> beans, CalculatorBean bean) {
        mAdapter.notifyDataSetChanged();
        mInput.setText(bean.number);
        mResult.setText(bean.result);

        if (bean.result.equals("") || bean.result.equals("0")) {
            mResult.setVisibility(View.GONE);
        } else {
            mResult.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void dataBaseRead(CalculatorBean bean) {

    }

    @Override
    public void dataBaseSave(CalculatorBean bean) {

    }

    @Override
    public void dataBaseDelete(CalculatorBean bean) {

    }



}
